package learnTengo

import (
	"fmt"
	"github.com/d5/tengo"
	"github.com/d5/tengo/stdlib"
)

var code = `
fmt := import("fmt")
fmt.println("this is main")
sum := import("sum")
fmt.println(sum, "====", is_callable(sum))
sum2 := import("sum2")
fmt.println(sum2, "====", is_callable(sum2))
`

func UseStdlibMain() {
	s := tengo.NewScript([]byte(code))
	modmap := stdlib.GetModuleMap(stdlib.AllModuleNames()...)
	modmap.AddSourceModule("sum", []byte(`
		fmt := import("fmt")
		fmt.println("this is sum")
		math := import("math")
		export math.e+math.sin(math.pi/2)
	`))
	modmap.AddSourceModule("sum2", []byte(`
		fmt := import("fmt")
		fmt.println("this is sum2")
		sum := import("sum")
		math := import("math")
		export sum+math.e
	`))

	s.SetImports(modmap)
	c, err := s.Compile()
	if err != nil {
		fmt.Println(err)
	}
	if err := c.Run(); err != nil {
		panic(err)
	}
}
